﻿import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Styles 1.4
import "../components"

Rectangle {

    id: root

    property int temperature: 60
    property int minT: 0
    property int maxT: 120
    property int time: 0
    property int rate: 0
    property int step: 0

    property bool editTemp: false


    property bool isEditable: false

    property bool isCollect: true
    property bool collectable: true

    height: parent.height
    width: 210*dp


    // 颜色控制(温度)
    Rectangle{
        color: "white"
        width: parent.width
        height: parent.height * Math.min(1 - temperature / maxT, 1)
    }
    gradient: Gradient{
        GradientStop {position: 1.0; color: "#E5F0FF"}
        GradientStop {position: 0.0; color: "#FFCCE3"}
    }

    function refresh(){
        canvas.requestPaint()

        var theta = 1.25*Math.PI - ((temperature-minT)/(maxT-minT) * 1.5*Math.PI)
        var x = 40*dp * Math.cos(theta) + 42*dp
        var y = -40*dp * Math.sin(theta) + 42*dp
        controller.x = x
        controller.y = y

    }

    MouseArea{
        anchors.fill: parent
        onReleased: {
            root.focus = true
        }
    }

    // 温度控制圆环
    Item{
        height: 100 *dp
        width: 100*dp
        y: 100*dp
        anchors.horizontalCenter: parent.horizontalCenter
        Canvas{
            id: canvas
            anchors.fill: parent
            onPaint: {
                var ctx = canvas.getContext("2d")
                ctx.clearRect(0, 0, height, width)
                ctx.lineCap = "round"
                ctx.beginPath()
                ctx.arc(50*dp, 50*dp, 40*dp, 0.75*Math.PI, 0.25*Math.PI)
                ctx.strokeStyle = "#333333"
                ctx.lineWidth = 10*dp
                ctx.stroke()

                ctx.beginPath()
                ctx.arc(50*dp, 50*dp, 40*dp, 0.75*Math.PI, ((temperature-minT)/(maxT-minT)*1.5 + 0.75) * Math.PI)
                ctx.strokeStyle = "#FF5900"
                ctx.lineWidth = 10*dp
                ctx.stroke()
            }
        }
        Item{
            height: 20*dp
            width: 40*dp
            anchors.centerIn: parent
            Text {
                text: temperature + "°C"
                font.pixelSize: config.fontsize + 2
                anchors.centerIn: parent
            }
            MouseArea{          // 温度区域双击变为可输入设置
                anchors.fill: parent
                cursorShape: Qt.IBeamCursor
                onReleased: {
                    if(!isEditable) return
                    editTemp = true
                    edit_temp.focus = true
                    edit_temp.setText(temperature)
                }
            }
        }LineEditor{
            id: edit_temp
            height: 30*dp
            editWidth: 50*dp
            visible: editTemp && isEditable
            inputMaxLength: 3
            maxLengthTip: false
            intflag: true
            anchors.centerIn: parent
            onTextChanged: {
                if(Number(getText()) < minT || Number(getText()) > maxT){
                    setText(Math.min(Math.max(Number(getText()), minT), maxT))
                    mainwindow.showToast("温度范围为"+minT+"到"+maxT+"°C", 1)
                }
                temperature = Math.min(Math.max(Number(getText()), minT), maxT)
                refresh()
            }onEditingFinished: {
                editTemp = false
                if(Number(getText()) < minT || Number(getText()) > maxT){
                    mainwindow.showToast("温度范围为"+minT+"到"+maxT+"°C", 1)
                }
                temperature = Math.min(Math.max(Number(getText()), minT), maxT)
                refresh()
            }
        }

        Rectangle{
            id: controller
            height: 16*dp
            width: 16*dp
            color: "#FF5900"
            radius: 8*dp
            Rectangle{
                height: 8*dp
                width: 8*dp
                radius: 4*dp
                color: "#333333"
                x: 4*dp
                y: 4*dp
            }
            MouseArea{          // 小圆
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onPressed: {
                    controller.focus = true
                    editTemp = false
                    if(!isEditable){
                        return
                    }
                    var mousePos = mapToItem(canvas, mouseX, mouseY)
                }
                onPositionChanged: {
                    if(!isEditable){
                        return
                    }

                    // 位置确定
                    var mousePos = mapToItem(canvas, mouseX, mouseY)
                    var offsetX = mousePos.x - 50*dp
                    var offsetY = mousePos.y - 50*dp

                    if(offsetY > 0 && Math.abs(offsetY) > Math.abs(offsetX)){
                        if(temperature > (maxT+minT)/2){
                            offsetX = 1
                            offsetY = 1
                        }else{
                            offsetX = -1
                            offsetY = 1
                        }
                    }
                    if(offsetX === 0){
                        offsetX = 0.00001
                    }if(offsetY === 0){
                        offsetY = 0.00001
                    }

                    var k = Math.abs(offsetY / offsetX)
                    var x = Math.sqrt(1600*dp*dp / (1 + k*k)) * Math.abs(offsetX)/(offsetX)
                    var y = k * Math.sqrt(1600*dp*dp / (1 + k*k)) * Math.abs(offsetY)/(offsetY)
                    controller.x = x-8*dp + 50*dp
                    controller.y = y-8*dp + 50*dp

                    if(x === 0){
                        x = 0.000001
                    }if(y === 0){
                        y = 0.000001
                    }

                    // 温度计算
                    var theta = Math.atan(  Math.abs((-1-y/x) / (1-y/x))  )
                    if(Math.abs(y) > Math.abs(x)){
                        theta = Math.PI - theta
                    }else if(x < 0){
                        theta += 0
                    }else if(x > 0){
                        theta += Math.PI * 1
                    }
                    temperature = theta / (Math.PI*1.5) * (maxT - minT) + minT
                    canvas.requestPaint()

                }

                onReleased: {
                    if(!isEditable){
                        return
                    }
                    var mousePos = mapToItem(canvas, mouseX, mouseY)
                }
            }
        }

    }

    // 时间
    LineEditor{
        id: edit_time
        anchors.horizontalCenter: parent.horizontalCenter
        y: 200*dp
        editWidth: 110*dp
        height: 38*dp
        defaultText: time
        inputMaxLength: 3
        editable: root.isEditable
        intflag: true
        onTextChanged: {
            time = edit_time.getText()
        }
        Text {
            id : tt
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            anchors.rightMargin: 10
            text: "时间(s)  "
            font.pixelSize: config.fontsize
            color: "#909090" // 灰色文字
        }
    }
    // 速率
    LineEditor{
        id: edit_rate
        anchors.horizontalCenter: parent.horizontalCenter
        y: 260*dp
        editWidth: 110*dp
        height: 38*dp
        defaultText: rate
        intflag: true
        inputMaxLength: 2
        editable: root.isEditable
        onTextChanged: {
            rate = edit_rate.getText()
        }
        Text {
            id : ss
            anchors.right: parent.right
            text: "速率(°C/s)"
            font.pixelSize: config.fontsize
            anchors.verticalCenter: parent.verticalCenter
            anchors.rightMargin: 10
            color: "#909090" // 灰色文字
        }
    }

    // 采集小相机
    Image{
        height: 30*dp
        width: 30*dp
        y: 315*dp
        anchors.horizontalCenter: parent.horizontalCenter
        visible: collectable
        source: isCollect ? "qrc:/picture/camera_collect.png" : "qrc:/picture/camera_uncollect.png"
        MouseArea{
            anchors.fill: parent
            cursorShape: Qt.PointingHandCursor
            hoverEnabled: true
            onEntered:{
                tooltip_camera.visible = true
            }onExited: {
                tooltip_camera.visible = false
            }
            onReleased: {
                root.focus = true
                if(!isEditable){
                    return
                }
                changeCollect(step, ! isCollect)
            }
        }
        ToolTip{
            id: tooltip_camera
            visible: false
            delay: 800
            x: 25*dp
            y: 30*dp
            background: Rectangle{
                color: "#222222"
                radius: 5
            }
            contentItem: Text{
                text: "荧光检测"
                color: "white"
                font.pixelSize: config.fontsize
            }
            opacity: 0.85
        }
    }

    // 下面的加减号
    Item{
        height: 45*dp
        width: 45*dp
        y: parent.height-45*dp
        Text {
            text: qsTr("—")
            color: "#1677FF"
            font.bold: true
            font.pixelSize: 40*dp
            anchors.centerIn: parent
        }MouseArea{
            anchors.fill: parent
            cursorShape: Qt.PointingHandCursor
            onReleased: {
                root.focus = true
                if(!isEditable){
                    return
                }
                delStep(step)
            }
        }
    }Item{
        height: 45*dp
        width: 45*dp
        y: parent.height-45*dp
        x: parent.width-45*dp
        Text {
            text: qsTr("+")
            color: "#1677FF"
            font.bold: true
            font.pixelSize: 40*dp
            anchors.centerIn: parent
        }MouseArea{
            anchors.fill: parent
            cursorShape: Qt.PointingHandCursor
            onReleased: {
                root.focus = true
                if(!isEditable){
                    return
                }
                addStep(step+1)
            }
        }
    }Item{
        height: 45*dp
        width: 45*dp
        y: parent.height-45*dp
        anchors.horizontalCenter: parent.horizontalCenter
        Text {
            text: step
            color: "white"
            font.bold: true
            font.pixelSize: 40*dp
            anchors.centerIn: parent
        }
    }




}
